
//
//  GWLrcView.m
//  QQ音乐
//
//  Created by qianfeng on 16/3/10.
//  Copyright © 2016年 高炜. All rights reserved.
//

#import "GWLrcView.h"
#import "Masonry.h"
#import "GWLrcTool.h"
#import "GWLrcCell.h"
#import "GWLrc.h"
#import "GWProgressLabel.h"
#import "GWMusic.h"
#import "GWPlayMusicTool.h"
#import <MediaPlayer/MediaPlayer.h>
#define myColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
@interface GWLrcView ()<UITableViewDataSource>
@property (nonatomic,weak)UITableView *lrcTableView;
@property (nonatomic,strong)NSArray *lrcArray;

@property (nonatomic,assign)NSInteger currentIndex;
@end
@implementation GWLrcView
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self =[super initWithCoder:aDecoder]) {
        [self setUpTableView];
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        [self setUpTableView];
    }
    return self;
}
-(void)setUpTableView
{
    UITableView *lrcTableView =[[UITableView alloc]init];
    
    lrcTableView.rowHeight =40;
    [self addSubview:lrcTableView];
    lrcTableView.dataSource =self;
    self.lrcTableView =lrcTableView;
    
    
}
-(void)setLrcName:(NSString *)lrcName
{
    _lrcName =[lrcName copy];
    _currentIndex =0;
    self.lrcArray =[GWLrcTool lrcArrayWithLrcFileName:lrcName];
    [self.lrcTableView reloadData];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.contentSize =CGSizeMake(self.frame.size.width*2, 0);
    
    [self.lrcTableView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(self.mas_top);
         make.left.equalTo(self.mas_left).offset(self.frame.size.width);
         make.bottom.equalTo(self.mas_bottom);
         make.right.equalTo(self.mas_right);
        make.width.equalTo(self.mas_width);
        make.height.equalTo(self.mas_height);
        
    }];
    self.lrcTableView.contentInset =UIEdgeInsetsMake(self.lrcTableView.frame.size.height*0.5, 0, self.lrcTableView.frame.size.height*0.5, 0);
     self.lrcTableView.backgroundColor =[UIColor clearColor];
     self.lrcTableView.separatorStyle =UITableViewCellSeparatorStyleNone;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.lrcArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GWLrcCell *cell =[GWLrcCell cellWithTableView:tableView];
    cell.lrc =self.lrcArray[indexPath.row];
    if (indexPath.row==self.currentIndex) {
        cell.progressLabel.font =[UIFont systemFontOfSize:20];
    }
    else
    {
        cell.progressLabel.font =[UIFont systemFontOfSize:14.0];
        cell.progressLabel.progress =0;
    }
    return cell;
}
-(void)setCurrentTime:(NSTimeInterval)currentTime
{
    _currentTime =currentTime;
    
    NSInteger count =self.lrcArray.count;
    for (int i =0; i<count; i++) {
        
        GWLrc *currentLrc =self.lrcArray[i];
        GWLrc *nextLrc =nil;
        if (i+1<count) {
            nextLrc =self.lrcArray[i+1];
        }
        NSTimeInterval current =currentLrc.lrcTime;
        NSTimeInterval next =nextLrc.lrcTime;
        
        if (currentTime >=current&&currentTime<next&&i!=self.currentIndex) {
            NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
            NSIndexPath *previousPath =[NSIndexPath indexPathForRow:i-1 inSection:0];
            [self.lrcTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
            self.currentIndex =i;
            [self.lrcTableView reloadRowsAtIndexPaths:@[indexPath,previousPath] withRowAnimation:UITableViewRowAnimationNone];
            self.homeLrcLable.text =currentLrc.lrcText;
            [self productionImage];
            return;
        }
        if (i==self.currentIndex) {
             NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
            CGFloat progress =(currentTime - current)/(next -current);
            GWLrcCell *cell =[self.lrcTableView cellForRowAtIndexPath:indexPath];
            cell.progressLabel.progress =progress;
            self.homeLrcLable.progress =progress;
        }
    }
}

-(void)productionImage
{
    GWMusic *currentMusic =[GWPlayMusicTool currentPlayingMusic];
    UIImage *image =[UIImage imageNamed:currentMusic.icon];
    GWLrc *currentLrc =self.lrcArray[self.currentIndex];
    NSInteger previousIndex =self.currentIndex-1;
    GWLrc *previousLrc =nil;
    if (previousIndex>0) {
        previousLrc =self.lrcArray[previousIndex];
    }
     NSInteger nextIndex =self.currentIndex+1;
    GWLrc *nextLrc =nil;
    if (nextIndex<self.lrcArray.count) {
        nextLrc =self.lrcArray[nextIndex];
    }
    UIGraphicsBeginImageContext(image.size);
    
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    CGFloat textH =25;
    NSMutableParagraphStyle *style =[[NSMutableParagraphStyle alloc]init];
    style.alignment =NSTextAlignmentCenter;
    NSDictionary *currentAttributes =@{
                                       NSFontAttributeName:[UIFont systemFontOfSize:16],
                                       NSForegroundColorAttributeName:myColor(80, 192, 120),
                                       NSParagraphStyleAttributeName:style
                                       };
    [currentLrc.lrcText drawInRect:CGRectMake(0, image.size.height -textH*2, image.size.width, textH) withAttributes:currentAttributes];
    
    NSDictionary *nextAttributes =@{
                                       NSFontAttributeName:[UIFont systemFontOfSize:14],
                                       NSForegroundColorAttributeName:[UIColor lightGrayColor],
                                       NSParagraphStyleAttributeName:style
                                       };
    [previousLrc.lrcText drawInRect:CGRectMake(0, image.size.height -textH*3, image.size.width, textH) withAttributes:nextAttributes];
    [nextLrc.lrcText drawInRect:CGRectMake(0, image.size.height -textH, image.size.width, textH) withAttributes:nextAttributes];
    UIImage *endImage =UIGraphicsGetImageFromCurrentImageContext();
    [self setLockScreenInfoWithImage:endImage];
    UIGraphicsEndImageContext();
}
/** 设置锁屏信息*/
-(void)setLockScreenInfoWithImage:(UIImage *)image
{
    GWMusic *currentMusic =[GWPlayMusicTool currentPlayingMusic];
    MPNowPlayingInfoCenter *playingInfo = [MPNowPlayingInfoCenter defaultCenter];
    NSMutableDictionary *infoDict =[NSMutableDictionary dictionary];
    [infoDict setObject:currentMusic.name forKey:MPMediaItemPropertyAlbumTitle];
    [infoDict setObject:currentMusic.singer forKey:MPMediaItemPropertyArtist];
    MPMediaItemArtwork *artwork =[[MPMediaItemArtwork alloc]initWithImage:image];
    [infoDict setObject:artwork forKey:MPMediaItemPropertyArtwork];
    [infoDict setObject:@(self.currentPlayer.duration) forKey:MPMediaItemPropertyPlaybackDuration];
     [infoDict setObject:@(self.currentPlayer.currentTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    playingInfo.nowPlayingInfo =infoDict;
    
    
    [[UIApplication sharedApplication]beginReceivingRemoteControlEvents];
}
@end
